package com.ease.framework.gef {
   import com.ease.common.models.IModel;
   import com.ease.framework.figures.IFigure;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;
   import com.ease.kernel.Kernel;

   import flash.geom.Point;
   import flash.ui.ContextMenu;
   import flash.ui.ContextMenuItem;
   import flash.utils.getQualifiedClassName;

   /**
    *
    * @author James Hu
    *
    */
   public class EditPart implements IEditPart {
      private var _model:IModel;
      private var _figure:IFigure;

      private var contextMenu:ContextMenu;

      public function EditPart(obj:IModel) {
         _model = obj;
         IEditor(Workbench.instance.currentEditor).getEditDomain().pushModel(model, this);
         obj.registerChangeListener(this);
      }

      public function get model():IModel {
         return _model;
      }

      public function createFigure():void {
         _figure = IEditor(Workbench.instance.currentEditor).getFigureFactory().createFigure(model);
         IEditor(Workbench.instance.currentEditor).getEditDomain().pushFigure(figure, this);
      }

      public function removeFigure():void {
         if (figure != null)
            IEditor(Workbench.instance.currentEditor).getEditDomain().removeFigureFromMap(figure);
      }

      final public function refreashFigure():void {

         doRefreashFigure();
         var manager:IProcessStateManager = IProcessStateManager(Kernel.getInstance(getQualifiedClassName(IProcessStateManager)).getService());
         if (manager) {
            manager.decorateFigure(_figure);
         }
      }

      public function doRefreashFigure():void {
         //子类覆盖
      }

      public function reloadFigure():void {

      }

      public function renderFigure():void {
      }

      public function reload():void {
      }

      public function remove():void {
         IEditor(Workbench.instance.currentEditor).getEditDomain().removeModelFromMap(model);
         IEditor(Workbench.instance.currentEditor).getEditDomain().removeFigureFromMap(figure);
      }

      public function unremove():void {
         IEditor(Workbench.instance.currentEditor).getEditDomain().pushModel(model, this);
         IEditor(Workbench.instance.currentEditor).getEditDomain().pushFigure(figure, this);
      }

      public function get figure():IFigure {
         if (_figure == null) {
            createFigure();
//				UIComponent(figure).contextMenu = getContextMenu();
         }
         return _figure;
      }

      public function notifyChanged(model:IModel):void {
         Workbench.instance.isDirty = true;
         refreashFigure();
      }


      public function createContextMenu():void {
         contextMenu = new ContextMenu();
         contextMenu.hideBuiltInItems();
         contextMenu.customItems.splice(0, contextMenu.customItems.length);
      }

      public function createCustomContextMenuItems():void {

      }

      public function pushCustomContextMenuItem(item:ContextMenuItem):void {
         contextMenu.customItems.push(item);
      }

      public function createNewCommand(pool:IModel, parent:IModel = null, point:Point = null):ICommand {

         return null;
      }

      public function createEditCommand(newModel:IModel):ICommand {

         return null;
      }

      public function createRemoveCommand():ICommand {

         return null;
      }

      public function createMoveResizeCommand(gap_x:Number, gap_y:Number, gap_width:Number, gap_height:Number):ICommand {

         return null;
      }

   }
}